﻿using System;
using System.Reflection;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;

using MyCtp.Model;
using MyCtp.Api;

using WAF.EntityFramework;

namespace TradeCore.Trade
{
    public class CtpTrade
    {
        /// <summary>
        /// CTP交易接口
        /// </summary>
        private TradeApi tdApi;


        /******************************** Public ********************************/

        //注册CTP交易接口事件
        public CtpTrade()
        {
            string borkerID = "4030";
            string userID = "80002863";
            string password = "888888";

            string[] frontTd = { @"tcp://asp-sim2-front1.financial-trading-platform.com:26205" };

            tdApi = new TradeApi(borkerID, userID, password, frontTd);

            tdApi.RtnOrder += OnRtnOrder;
            tdApi.RtnTrade += OnRtnTrade;
            tdApi.RtnInstrumentStatus += OnRtnInstrumentStatus;

            tdApi.ErrRtnOrderInsert += OnErrRtnOrderInsert;
            tdApi.ErrRtnOrderAction += OnErrRtnOrderAction;

            tdApi.RspOrderInsert += OnRspOrderInsert;
            tdApi.RspOrderAction += OnRspOrderAction;

            tdApi.RspQryOrder += OnRspQryOrder;
            tdApi.RspQryTrade += OnRspQryTrade;
            tdApi.RspQryTradingAccount += OnRspQryTradingAccount;
            tdApi.RspQryInvestorPosition += OnRspQryInvestorPosition;
            tdApi.RspQryInvestorPositionDetail += OnRspQryInvestorPositionDetail;
        }

        /// <summary>
        /// 买入开仓
        /// </summary>
        /// <param name="policy">策略</param>
        /// <param name="code">代码</param>
        /// <param name="price">价格</param>
        /// <param name="volume">数量</param>
        public void Buy(string policy, string code, double price, int volume)
        { 
            
        }

        /// <summary>
        /// 卖出开仓
        /// </summary>
        /// <param name="policy">策略</param>
        /// <param name="code">代码</param>
        /// <param name="price">价格</param>
        /// <param name="volume">数量</param>
        public void Sell(string policy, string code, double price, int volume)
        {

        }

        /// <summary>
        /// 平仓
        /// </summary>
        /// <param name="policy">策略</param>
        /// <param name="code">代码</param>
        /// <param name="price">价格</param>
        /// <param name="volume">数量</param>
        public void Close(string policy, string code, double price, int volume)
        {

        }


        /******************************** private ********************************/

        //报单响应
        private void OnRtnOrder(CThostFtdcOrderField pOrder)
        {

        }

        //报单成交通知
        private void OnRtnTrade(CThostFtdcTradeField pTrade)
        {

        }

        //报单录入请求响应
        private void OnRspOrderInsert(CThostFtdcInputOrderField pInputOrder, CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast)
        {

        }

        //报单操作请求响应
        private void OnRspOrderAction(CThostFtdcInputOrderActionField pInputOrderAction, CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast)
        {

        }

        //报单录入错误回报
        private void OnErrRtnOrderInsert(CThostFtdcInputOrderField pInputOrder, CThostFtdcRspInfoField pRspInfo)
        {

        }

        //报单操作错误回报
        private void OnErrRtnOrderAction(CThostFtdcOrderActionField pOrderAction, CThostFtdcRspInfoField pRspInfo)
        {

        }

        //请求查询报单响应
        private void OnRspQryOrder(CThostFtdcOrderField pOrder, CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast)
        {

        }

        //请求查询成交响应
        private void OnRspQryTrade(CThostFtdcTradeField pTrade, CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast)
        {

        }

        //查询资金账户响应
        private void OnRspQryTradingAccount(CThostFtdcTradingAccountField pTradingAccount, CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast)
        {

        }

        //查询投资者持仓响应
        private void OnRspQryInvestorPosition(CThostFtdcInvestorPositionField pInvestorPosition, CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast)
        {

        }

        //查询投资者持仓明细响应
        private void OnRspQryInvestorPositionDetail(CThostFtdcInvestorPositionDetailField pInvestorPositionDetail, CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast)
        {

        }

        //合约交易状态通知
        private void OnRtnInstrumentStatus(CThostFtdcInstrumentStatusField pInstrumentStatus)
        {

        }
    }
}
